package algorithm.view.sort.select;

import algorithm.view.common.AlgoHelpler;
import java.awt.*;

/**
 * @author bingo
 * @Description
 * @Date 2018/4/29
 */
public class AlgoVisualizer {

    private  static  final  int  T = 15;
    private AlgoFrame algoFrame;

    private SelectSortData selectSortData ;


    public AlgoVisualizer(int senceWeight,int senceHeight,int N,int maxBound) {
        selectSortData = new SelectSortData(N,maxBound);
        EventQueue.invokeLater(()-> {
            algoFrame = new AlgoFrame("选择排序可视化", senceWeight, senceHeight);
            new Thread(()->{
                /**排序前渲染一次*/
                setData(0,-1,-1);
                for(int i=0;i<selectSortData.N();i++){
                    int minIndex = i;
                    setData(i,minIndex,-1);
                    for(int j=i+1;j<selectSortData.N();j++){
                         setData(i,minIndex,j);
                        if(selectSortData.get(minIndex)>selectSortData.get(j)){
                            minIndex=j;
                            setData(i,minIndex,j);
                        }

                    }
                    selectSortData.swap(i,minIndex);
                    setData(i+1,-1,-1);

                }
                /**排序完渲染一次*/
                setData(selectSortData.N(),-1,-1);
            }).start();

        });
    }

    public void setData(int orderIndex,int curentMinIndex,int compareToIndex){
        selectSortData.orderedIndex = orderIndex;
        selectSortData.curentMinIndex = curentMinIndex;
        selectSortData.compareToIndex = compareToIndex;
        algoFrame.render(selectSortData);
        AlgoHelpler.pause(T);

    }
}
